
package shthon2011s.eroid;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

import jp.co.sharp.android.hardware.Pedometer;

public class EroidActivity extends Activity {

    private TextView mTitle;

    private TextView mSteps;

    private TextView mCalorie;

    private Pedometer mPedometer;

    private final Handler mHandler = new Handler();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.eroid);

        mTitle = (TextView) findViewById(R.id.title);
        setTypeface(mTitle);
        mSteps = (TextView) findViewById(R.id.steps);
        setTypeface(mSteps);
        mCalorie = (TextView) findViewById(R.id.calorie);
        setTypeface(mCalorie);

        mPedometer = Pedometer.createInstance(this);
        if (mPedometer == null) {
            Toast.makeText(this, "歩数計APIが使用できません", Toast.LENGTH_LONG).show();
            finish();
            return;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);

        final MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_menu, menu);
        return true;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        final boolean handled;

        switch (item.getItemId()) {
            case R.id.menu_ranking: {
                final Intent intent = new Intent(this, RankingActivity.class);
                startActivity(intent);

                handled = true;
                break;
            }
            case R.id.menu_train: {
                final Intent intent = new Intent(this, DenshaActivity.class);
                final int steps = mPedometer.getIntParameter(Pedometer.STEPS);
                intent.putExtra(DenshaActivity.STEPS, steps);
                final float calorie = mPedometer.getFloatParameter(Pedometer.CALORIE);
                intent.putExtra(DenshaActivity.CALORIE, calorie);

                startActivity(intent);

                handled = true;
                break;
            }
            case R.id.menu_pedometer: {
                final Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.addCategory(Intent.CATEGORY_LAUNCHER);
                intent.setClassName("jp.co.sharp.android.pedometer",
                        "jp.co.sharp.android.pedometer.PedometerMainActivity");
                startActivity(intent);
                handled = true;
                break;
            }
            default:
                handled = false;
                break;
        }

        return super.onMenuItemSelected(featureId, item) || handled;
    }

    private static final int REQ_NAME_REGISTRATION = 1;

    @Override
    protected void onStart() {
        super.onStart();

        final SharedPreferences preferences = SettingsActivity.getSharedPreferences(this);
        final String name = preferences.getString(SettingsActivity.PREF_KEY_NAME, null);
        if (name == null) {
            final Intent intent = new Intent(this, SettingsActivity.class);
            startActivityForResult(intent, REQ_NAME_REGISTRATION);
            return;
        }

        final String title = getString(R.string.profile_title, name);
        mTitle.setText(title);

        //        mHandler.postDelayed(new Runnable() {
        //            @Override
        //            public void run() {
        //                openOptionsMenu();
        //            }
        //        }, 100L);
    }

    private void updateData(int steps, float calorie) {
        final String stepsTxt = getString(R.string.steps, Integer.valueOf(steps));
        mSteps.setText(stepsTxt);
        final String caloriesTxt = getString(R.string.calories, Float.valueOf(calorie));
        mCalorie.setText(caloriesTxt);
    }

    @Override
    protected void onResume() {
        super.onResume();

        // 歩数のintの値を取得
        int steps = mPedometer.getIntParameter(Pedometer.STEPS);
        // カロリーと移動距離のfloatの値を取得
        float calorie = mPedometer.getFloatParameter(Pedometer.CALORIE);
        updateData(steps, calorie);

        final IntentFilter filter = new IntentFilter();
        filter.addAction(Pedometer.ACTION_MEASURE);
        registerReceiver(mPedometerReceiver, filter);
        //インテント通知要求
        mPedometer.startBroadcastIntent();
    }

    @Override
    protected void onPause() {
        super.onPause();

        mPedometer.stopBroadcastIntent();
        unregisterReceiver(mPedometerReceiver);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case REQ_NAME_REGISTRATION:
                if (resultCode != Activity.RESULT_OK) {
                    finish();
                }
                break;
        }
    }

    private final BroadcastReceiver mPedometerReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (!intent.getAction().equals(Pedometer.ACTION_MEASURE)) {
                return;
            }

            final int steps = intent.getIntExtra(Pedometer.STEPS, 0);
            final float calorie = intent.getFloatExtra(Pedometer.CALORIE, 0.0f);

            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    updateData(steps, calorie);
                }
            });
        }
    };

    private Typeface mTypefaceForTitle;

    private void setTypeface(TextView tv) {
        if (mTypefaceForTitle == null) {
            //mTypefaceForTitle = Typeface.create("MRSW-ShinGo-Regular", Typeface.NORMAL);
            mTypefaceForTitle = Typeface.create("SH-LCMincho-MP", Typeface.NORMAL);
        }
        tv.setTypeface(mTypefaceForTitle);
    }
}
